#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QDockWidget>
#include <QPointer>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow) {
    ui->setupUi(this);


    AccountManager();
    QWidget *mainContainer = new QWidget(this);

    // Create a vertical layout for the main container
    QVBoxLayout *mainLayout = new QVBoxLayout(mainContainer);

    // Initialize the login status widget
    loginStatusWidget = new LoginStatusWidget(this);
    loginStatusWidget->setFixedSize(200, 15);  // Set a fixed size to reduce empty space

    // Create a dock widget to contain the login status widget
    QDockWidget *dockWidget = new QDockWidget(this);
    dockWidget->setWidget(loginStatusWidget);
    dockWidget->setAllowedAreas(Qt::LeftDockWidgetArea | Qt::TopDockWidgetArea);
    dockWidget->setTitleBarWidget(new QWidget());  // Remove the title bar
    dockWidget->setFeatures(QDockWidget::NoDockWidgetFeatures);

    // Add the dock widget to the top-left corner
    addDockWidget(Qt::TopDockWidgetArea, dockWidget);

    // Add the user management button
    QPushButton *userManagementButton = new QPushButton("用户管理", this);
    userManagementButton->setFixedSize(100, 30);  // Set button size as needed

    // Create a dock widget to contain the user management button
    QDockWidget *userDockWidget = new QDockWidget(this);
    userDockWidget->setWidget(userManagementButton);
    userDockWidget->setAllowedAreas(Qt::RightDockWidgetArea | Qt::TopDockWidgetArea);
    userDockWidget->setTitleBarWidget(new QWidget());  // Remove the title bar
    userDockWidget->setFeatures(QDockWidget::NoDockWidgetFeatures);

    userManagementWidget = new UserManagementWidget(this);
    userManagementWidget->hide();

    // Add the dock widget to the top-right corner
    addDockWidget(Qt::TopDockWidgetArea, userDockWidget);

    // Connect the button to open the UserManagementWidget
    connect(userManagementButton, &QPushButton::clicked, this, &MainWindow::showUserManagement);

    // Initialize stacked widget and add main, map, and result widgets
    stackedWidget = new QStackedWidget(this);
    mainWidget = new MainWidget(this);
    mapWidget = new MapWidget(this);
    resultWidget = new ResultWidget(this);

    stackedWidget->addWidget(mainWidget);
    stackedWidget->addWidget(mapWidget);
    stackedWidget->addWidget(resultWidget);

    // Add the stacked widget to the main layout
    mainLayout->addWidget(stackedWidget, 1);  // Stretch factor 1 to fill remaining space

    // Set the main layout as the central widget
    setCentralWidget(mainContainer);

    // Connect signals and slots for switching between pages
    connect(mainWidget, &MainWidget::openResult, this, &MainWindow::showResultWidget);
    connect(mainWidget, &MainWidget::showLogin,this,&MainWindow::showLogin);
    connect(mapWidget, &MapWidget::backMain, this, &MainWindow::showMainWidget);
    connect(resultWidget, &ResultWidget::backMain, this, &MainWindow::showMainWidget);
    connect(resultWidget, &ResultWidget::bookingCompleted, mapWidget, &MapWidget::updateMapConnections);

}

void MainWindow::showUserManagement() {
    if (!SessionManager::getIsLogin()) {
        QMessageBox::warning(this, "未登录", "请先登录。");
        return;
    }
    else{
            UserManagementWidget *userManagementWidget = new UserManagementWidget(this);
            userManagementWidget->setAttribute(Qt::WA_DeleteOnClose);  // Close to free memory
            userManagementWidget->setWindowFlags(Qt::Window | Qt::WindowTitleHint | Qt::WindowCloseButtonHint);
            userManagementWidget->setWindowModality(Qt::ApplicationModal);  // Make it stay on top
            userManagementWidget->show();
            connect(userManagementWidget, &UserManagementWidget::openMap, this, &MainWindow::showMapWidget);
            connect(userManagementWidget, &UserManagementWidget::deletingCompleted, mapWidget, &MapWidget::updateMapConnections);
    }
}

MainWindow::~MainWindow() {
    // Qt 会自动管理 ui 对象的删除
    // 不需要手动 delete
}

void MainWindow::showLogin() {
    if (!loginDialog) {
        // 只创建一次 LoginDialog
        loginDialog = new LoginDialog(this);
        connect(loginDialog, &LoginDialog::loginSuccessful, this, &MainWindow::handleLoginSuccess, Qt::QueuedConnection);
    }

    loginDialog->setWindowModality(Qt::ApplicationModal);
    loginDialog->show();
}


void MainWindow::handleLoginSuccess(const QString &username,const QString &password) {
     AccountManager accountManager;
    QString userID = accountManager.loginUser(username, password);
    if (!userID.isEmpty()) {
        loginStatusWidget->updateLoginStatus(username);
        SessionManager::setUserID(userID);  // 保存 userID
        SessionManager::setUsername(username);
        SessionManager::setIsLogin(true);

        if (loginDialog) {
            loginDialog->close();
        }
    } else {
        qDebug() << "登录失败";
    }
}


void MainWindow::showMapWidget() {
    stackedWidget->setCurrentWidget(mapWidget);
}

void MainWindow::showMainWidget() {
    stackedWidget->setCurrentWidget(mainWidget);
}

void MainWindow::showResultWidget() {
    stackedWidget->setCurrentWidget(resultWidget);
}


